﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KabiManagementWeb.Models;
namespace KabiManagementWeb.Controllers
{
    public class ProductController : Controller
    {
        KBManagementDataContext context = DBUtils.GetContext(DBUtils.DBType.Local);
        //
        // GET: /Product/

        public ActionResult Index(string id)
        {
            if (id == "KAR" || id == "BIA")
            {
                if (id == "KAR")
                {
                    ViewData["type"] = "các phòng Karaoke";
                }
                else
                {
                    ViewData["type"] = "các bàn BI-A";
                }
                return View(context.Products.Where(x => x.CategoryID.Equals(id)));
            }

            return View(context.Products);
        }



        //
        // GET: /Product/Create

        public ActionResult Create()
        {
            ViewData["category"] = new SelectList(context.Categories.ToList<Category>(), "ID", "Name");
            return View();
        }

        //
        // POST: /Product/Create

        [HttpPost]
        public ActionResult Create(Product collection)
        {
            try
            {
                String url = "";
                // TODO: Add insert logic here
                if (collection.CategoryID == "KAR")
                {
                    collection.ImageUsePath = "KAR";
                    collection.ImageNotUsePath = "KAR1";
                    url = "KAR";
                }
                if (collection.CategoryID == "BIA")
                {
                    collection.ImageUsePath = "images";
                    collection.ImageNotUsePath = "images1";
                    url = "BIA";
                }
                collection.Status = true;
                context.Products.InsertOnSubmit(collection);
                context.SubmitChanges();
                return RedirectToAction("Index", new { id=url});
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Product/Edit/5

        public ActionResult Edit(int id)
        {

            ViewData["category"] = new SelectList(context.Categories.ToList(), "ID", "Name");
            return View(context.Products.Single(x => x.ID == id));
        }

        //
        // POST: /Product/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Product collection)
        {
            try
            {
                // TODO: Add update logic here
                var pro = context.Products.Single(x => x.ID == id);
                pro.Name = collection.Name;
                pro.CategoryID = collection.CategoryID;
                pro.Price = collection.Price;
                String url = "";
                if (collection.CategoryID == "KAR")
                {
                    pro.ImageUsePath = "KAR";
                    pro.ImageNotUsePath = "KAR1";
                    url = "KAR";
                }
                if (collection.CategoryID == "BIA")
                {
                    pro.ImageUsePath = "images";
                    pro.ImageNotUsePath = "images1";
                    url = "BIA";
                }
                context.SubmitChanges();
                return RedirectToAction("Index", new { id=url});
            }
            catch
            {
                return View();
            }
        }


    }
}
